package com.ljfiction.book.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ljfiction.book.Baner.SearchTuijianBean;
import com.ljfiction.book.Dialog.NoBackgLodDialog;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.R;
import com.ljfiction.book.activitys.BookDetailsActivity;
import com.ljfiction.book.activitys.SearchActivity;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.StringTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;


public class SearchFragment extends BaseFragment {


    @BindView(R.id.daohanglan_search)
    LinearLayout daohanglanSearch;

    Unbinder unbinder;
    @BindView(R.id.search_edittextview)
    TextView searchEdittextview;
    @BindView(R.id.search_tuijianrecycle)
    RecyclerView searchTuijianrecycle;
    @BindView(R.id.search_smart)
    SmartRefreshLayout searchSmart;
    private Intent intent;
    private SearchTuijianBean searchbean;
    private List<SearchTuijianBean.DataBean> datas = new ArrayList<>();
    private BaseQuickAdapter mAdapter;
    private Context context;
    private NoBackgLodDialog noBackgLodDialog;
    private boolean isfirst=false;

    @Override
    @Nullable
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search, container, false);
        unbinder = ButterKnife.bind(this, view);
        context = getContext();
        intView();
        intData();
        return view;
    }

    private void intView() {
        searchTuijianrecycle.setAdapter(mAdapter = new BaseQuickAdapter<SearchTuijianBean.DataBean, BaseViewHolder>(R.layout.morexiaobian_item, datas) {
            @Override
            protected void convert(BaseViewHolder helper, SearchTuijianBean.DataBean item) {
                AutoUtils.autoSize(helper.itemView);
                Glide.with(mContext).load(item.getBookImage()).into((ImageView) helper.getView(R.id.bookimage_morexiaobian));
                helper.setText(R.id.bookname_morexiaobian, item.getBookName())
                        .setText(R.id.clicknum_morexiaobian, StringTool.getClicknum(item.getClickTotal()) + "点击")
                        .setText(R.id.writername_morexiaobian, "作者: " + item.getWriterName())
                        .setText(R.id.bookclassname_morexiaobian, item.getClassificationName())
                        .setText(R.id.bookcomment_morexiaobian, item.getBookIntroduction());
                ((RecyclerView) helper.getView(R.id.booklable_morexiaobian)).setAdapter(new BaseQuickAdapter<SearchTuijianBean.DataBean.BooklableListBean, BaseViewHolder>(R.layout.homhotnewrecycal, item.getBooklableList()) {
                    @Override
                    protected void convert(BaseViewHolder helper, SearchTuijianBean.DataBean.BooklableListBean item) {
                        AutoUtils.autoSize(helper.itemView);
                        helper.setText(R.id.lablebordertext, item.getBookLableName()).setTextColor(R.id.lablebordertext, Color.parseColor(item.getBookColor()));
                    }
                });
                ((RecyclerView) helper.getView(R.id.booklable_morexiaobian)).setLayoutManager(new GridLayoutManager(mContext, 5));
                if (item.getBookStatus() == 1) {
                    helper.setText(R.id.zhuatai_morexiaobian, "已完结").setTextColor(R.id.zhuatai_morexiaobian, Color.parseColor("#FF6F00"));
                } else {
                    helper.setText(R.id.zhuatai_morexiaobian, "连载中").setTextColor(R.id.zhuatai_morexiaobian, Color.parseColor("#47B2D8"));
                }
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                intent = new Intent(context, BookDetailsActivity.class);
                intent.putExtra("bookId", String.valueOf(datas.get(position).getBookId()));
                startActivity(intent);
            }
        });
        searchTuijianrecycle.setLayoutManager(new LinearLayoutManager(context));
        searchTuijianrecycle.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL_LIST));
        searchSmart.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                datas.clear();
                intData();
            }
        });

    }

    private void intData() {
        OkHttpUtils.get(HttpManager.IP + "/sys-getRecommSearchPosition")
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        if (noBackgLodDialog==null){
                            noBackgLodDialog=new NoBackgLodDialog(context,true);
                        }
                        noBackgLodDialog.show();
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        noBackgLodDialog.dismis();
                        searchSmart.finishRefresh();
                        Gson g = new Gson();
                        try {
                            searchbean = g.fromJson(s, SearchTuijianBean.class);
                            if (searchbean.getReturnCode() == 200) {
                                datas.addAll(searchbean.getData());
                                mAdapter.notifyDataSetChanged();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.search_edittextview)
    public void onViewClicked() {
        intent = new Intent(context, SearchActivity.class);
        startActivity(intent);
    }
}
